package com.lezhi.socialgram.service.sys.impl;

import com.lezhi.socialgram.enums.ResultEnum;
import com.lezhi.socialgram.exception.SocialGramException;
import com.lezhi.socialgram.mapper.sys.UserPrivacyMapper;
import com.lezhi.socialgram.mapper.sys.UserPushMapper;
import com.lezhi.socialgram.model.sys.UserPrivacy;
import com.lezhi.socialgram.model.sys.UserPush;
import com.lezhi.socialgram.service.sys.UserSettingsService;
import com.lezhi.socialgram.util.Result;
import com.lezhi.socialgram.util.ResultUtils;
import com.lezhi.socialgram.util.TableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by daizhen on 2017/4/5.
 */
@Service
public class UserSettingsServiceImpl implements UserSettingsService {

    private static final Logger logger = LoggerFactory.getLogger(UserSettingsServiceImpl.class);

    @Autowired
    UserPrivacyMapper userPrivacyMapper;

    @Autowired
    UserPushMapper userPushMapper;

    @Override
    public Result setUserPrivacy(UserPrivacy userPrivacy) {
        try {
            userPrivacy.setTable(TableUtils.getTableNameWithId(UserPrivacy.class, userPrivacy.getUserId(), 10));
            UserPrivacy userPrivacy_ = userPrivacyMapper.queryById(userPrivacy);
            if (userPrivacy_ == null) {
                userPrivacyMapper.insertUserPrivacy(userPrivacy);
            } else {
                userPrivacyMapper.updateUserPrivacy(userPrivacy);
            }
            return ResultUtils.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SocialGramException(ResultEnum.INTERNAL_ERROR);
        }
    }

    @Override
    public Result queryPrivacyById(Long userId) {
        try {
            UserPrivacy userPrivacy = new UserPrivacy(TableUtils.getTableNameWithId(UserPrivacy.class, userId, 10),
                    userId);
            userPrivacy = userPrivacyMapper.queryById(userPrivacy);
            return ResultUtils.success(userPrivacy);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SocialGramException(ResultEnum.INTERNAL_ERROR);
        }
    }

    @Override
    public Result setUserPush(UserPush userPush) {
        try {
            userPush.setTable(TableUtils.getTableNameWithId(UserPush.class, userPush.getUserId(), 10));
            UserPush userPush_ = userPushMapper.queryById(userPush);
            if (userPush_ == null) {
                userPushMapper.insertUserPush(userPush);
            } else {
                userPushMapper.updateUserPush(userPush);
            }
            return ResultUtils.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SocialGramException(ResultEnum.INTERNAL_ERROR);
        }
    }

    @Override
    public Result queryPushById(Long userId) {
        try {
            UserPush userPush = new UserPush(TableUtils.getTableNameWithId(UserPush.class, userId, 10), userId);
            userPush = userPushMapper.queryById(userPush);
            return ResultUtils.success(userPush);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SocialGramException(ResultEnum.INTERNAL_ERROR);
        }
    }
}
